package org.jeecg.modules.demo.tcc.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.demo.tcc.entity.FuturesInventoryInfoSyn;
import org.jeecg.modules.demo.tcc.mapper.FuturesInventoryInfoSynMapper;
import org.jeecg.modules.demo.tcc.service.IFuturesInventoryInfoSynService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class FuturesInventoryInfoSynServiceImpl
        extends ServiceImpl<FuturesInventoryInfoSynMapper, FuturesInventoryInfoSyn>
        implements IFuturesInventoryInfoSynService {

    private FuturesInventoryInfoSynMapper synMapper;

    /**
     * 通过品种，获取当前库存在2020-2022年的百分比
     * @author:80006206
     * @Date:2022-01-21 0:54
     * @Description:
     * @return
     */
    @Override
     public JSONObject calCurStockPercent(){
         JSONObject ret = new JSONObject();

         //查询当前品种同步表信息
         FuturesInventoryInfoSyn syn = new FuturesInventoryInfoSyn();
         syn.setFuturesVarieties("JM");
         List<FuturesInventoryInfoSyn> synList = synMapper.selectAllByFuturesInventoryInfoSyn(syn);
         if (synList.size() < 1){
             ret.put("code","9999");
             ret.put("msg","查不到"+syn.getFuturesVarieties()+"对应的记录");
             return ret;
         }
         //获得当前日期
         String curDateStr = DateUtils.getDate("yyyy-MM-dd");

         //从当前日期的最近5天的库存数据
        FuturesInventoryInfoSyn synInfo = synList.get(0);
        String dateCollStr = synInfo.getDateCollection();
        String year2020CollStr = synInfo.getYear2020();
        String year2021CollStr = synInfo.getYear2021();
        String year2022CollStr = synInfo.getYear2022();
        JSONArray dateArr = JSONArray.parseArray(dateCollStr);
        JSONArray year2020Arr = JSONArray.parseArray(year2020CollStr);
        JSONArray year2021Arr = JSONArray.parseArray(year2021CollStr);
        JSONArray year2022Arr = JSONArray.parseArray(year2022CollStr);
        int avg = 0;
        int sum = 0;
        if (dateArr.contains(curDateStr)){
            //获得日期的下表
            int index = dateArr.indexOf(curDateStr);
            //获得2022年日期对应的库存
            String curStock0 = (String) year2022Arr.get(index);
            String curStockMin1 = (String) year2022Arr.get(index-1);
            String curStockMin2 = (String) year2022Arr.get(index-2);
            String curStockMin3 = (String) year2022Arr.get(index-3);
            String curStockMin4 = (String) year2022Arr.get(index-4);
            List<Integer> sumStock = new ArrayList<>();
            if (!"NaN".equals(curStock0)){
                sumStock.add(Integer.parseInt(curStock0));
            }
            if (!"NaN".equals(curStockMin1)){
                sumStock.add(Integer.parseInt(curStockMin1));
            }
            if (!"NaN".equals(curStockMin2)){
                sumStock.add(Integer.parseInt(curStockMin2));
            }
            if (!"NaN".equals(curStockMin3)){
                sumStock.add(Integer.parseInt(curStockMin3));
            }
            if (!"NaN".equals(curStockMin4)){
                sumStock.add(Integer.parseInt(curStockMin4));
            }
            int num = sumStock.size();
            for (Integer stock : sumStock) {
                sum += stock.byteValue();
            }
            avg = sum/num;
        }

         //加总2020-2022所有库存，取最大和最小值
        year2020Arr.fluentRemove("NAN");
        year2021Arr.fluentRemove("NAN");
        year2022Arr.fluentRemove("NAN");

        String[] arr = year2020Arr.getObject(0,String[].class);
        String min = Collections.min(Arrays.asList(arr));
        String max = Collections.max(Arrays.asList(arr));

         //计算百分比
         return ret;
     }


}




